﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using Jy123.Web.AppCode;
using Jy123.Common;
namespace Jy123.Web.Manage
{
    public partial class Star : MgConfig
    {
        private Dictionary<int, Jy123.Model.Public_SportType> sportTypeDic = new Dictionary<int, Jy123.Model.Public_SportType>();
        private Dictionary<int, Jy123.Model.Public_StarType> starTypeDic = new Dictionary<int, Jy123.Model.Public_StarType>();
        private Dictionary<int, Jy123.Model.Public_Level> levelDic = new Dictionary<int, Jy123.Model.Public_Level>();
        private List<Jy123.Model.Public_SportType> listSportType;
        private List<Jy123.Model.Public_StarType> listStarType;
        private List<Jy123.Model.Public_Level> listLevel;
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("Star.aspx");
            Jy123.BLL.Public_SportType BLLSportType = new Jy123.BLL.Public_SportType();
            listSportType = BLLSportType.GetModelList(string.Empty);
            Jy123.BLL.Public_StarType BLLStarType = new Jy123.BLL.Public_StarType();
            listStarType = BLLStarType.GetModelList(string.Empty);
            Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
            listLevel = BLLLevel.GetModelList(string.Empty);
            foreach (Jy123.Model.Public_SportType type in listSportType)
            {
                sportTypeDic.Add(type.ID, type);
            }
            foreach (Jy123.Model.Public_StarType starType in listStarType)
            {
                starTypeDic.Add(starType.ID, starType);
            }
            foreach (Jy123.Model.Public_Level level in listLevel)
            {
                levelDic.Add(level.ID, level);
            }
            if (!Page.IsPostBack)
            {
                LoadInitialValue();
            }
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_Star star = e.Item.DataItem as Jy123.Model.Public_Star;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = star.ID.ToString();
                Label sportTypeLabel = e.Item.FindControl("sportTypeLabel") as Label;
                Label typeLabel = e.Item.FindControl("typeLabel") as Label;
                Label nameLabel = e.Item.FindControl("nameLabel") as Label;
                Label skillLabel = e.Item.FindControl("skillLabel") as Label;
                Label fitnessLabel = e.Item.FindControl("fitnessLabel") as Label;
                Label livenessLabel = e.Item.FindControl("livenessLabel") as Label;
                Label moralLabel = e.Item.FindControl("moralLabel") as Label;
                Label expLabel = e.Item.FindControl("expLabel") as Label;
                Label priceLabel = e.Item.FindControl("priceLabel") as Label;
                Label usingLevelLabel = e.Item.FindControl("usingLevelLabel") as Label;
                if (star.FSportType.HasValue)
                {
                    sportTypeLabel.Text = sportTypeDic[star.FSportType.Value].SName;
                }
                if (star.FSType.HasValue)
                {
                    typeLabel.Text = starTypeDic[star.FSType.Value].SName;
                }

                nameLabel.Text = star.SName;

                if (star.Skill.HasValue)
                {
                    skillLabel.Text = star.Skill.ToString();
                }
                if (star.PhysicalFitness.HasValue)
                {
                    fitnessLabel.Text = star.PhysicalFitness.ToString();
                }
                if (star.Liveness.HasValue)
                {
                    livenessLabel.Text = star.Liveness.ToString();
                }
                if (star.Moral.HasValue)
                {
                    moralLabel.Text = star.Moral.ToString();
                }
                if (star.Experience.HasValue)
                {
                    expLabel.Text = star.Experience.Value.ToString();
                }
                if (star.Price.HasValue)
                {
                    priceLabel.Text = (Convert.ToInt32(star.Price.Value)).ToString();
                }
                string strUL = star.UsingLevel;
                if (!string.IsNullOrEmpty(strUL))
                {
                    string strResult = string.Empty;
                    string[] strArray = strUL.Split(',');
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(strArray[i]))
                        {
                            int id = int.Parse(strArray[i]);
                            strResult += levelDic[id].LName + ",";
                        }
                    }
                    if (strResult.Length > 1)
                    {
                        usingLevelLabel.Text = strResult.Remove(strResult.LastIndexOf(","));
                    }

                }
            }
        }

        protected void StarOptionRepeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_StarQueueName queueName = e.Item.DataItem as Jy123.Model.Public_StarQueueName;
                HiddenField idHidden = e.Item.FindControl("idHiddenCondition") as HiddenField;
                idHidden.Value = queueName.ID.ToString();
                CheckBox nameCheckBox = e.Item.FindControl("cNameCheckBox") as CheckBox;
                nameCheckBox.Text = queueName.QueueName;
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.Public_Star BLLStar = new Jy123.BLL.Public_Star();
            if (e.CommandName.Equals("Edit"))
            {
                Jy123.Model.Public_Star star = BLLStar.GetModel(id);
                actionTypeHidden.Value = "Update";
                idHiddenEdit.Value = id.ToString();
                iconImg.Src = star.SmallModel;
                JTypeImage.Src = star.Model;
                if (star.FSportType.HasValue)
                {
                    sportTypeDropDown.SelectedValue = star.FSportType.Value.ToString();
                    sportTypeDropDown_Change(sportTypeDropDown, EventArgs.Empty);
                }
                if (star.FSType.HasValue)
                {
                    typeDropDown.SelectedValue = star.FSType.Value.ToString();
                    if (typeDropDown.SelectedItem.ToString().Equals("球星"))
                    {
                        if (star.Experience.HasValue)
                        {
                            expTextBox.Text = star.Experience.Value.ToString();
                        }
                        string[] condition = star.Description.Split(',');
                        string[] values = star.DValues.Split(',');
                        for (int j = 0; j < StarOptionRepeater.Items.Count; j++)
                        {
                            RepeaterItem optionItem = StarOptionRepeater.Items[j];
                            CheckBox checkBox = optionItem.FindControl("cNameCheckBox") as CheckBox;
                            TextBox textBox = optionItem.FindControl("valueTextBox") as TextBox;
                            HiddenField idHidden = optionItem.FindControl("idHiddenCondition") as HiddenField;
                            RequiredFieldValidator required = optionItem.FindControl("valueRequired") as RequiredFieldValidator;
                            for (int i = 0; i < condition.Length; i++)
                            {
                                if (condition[i].Equals(idHidden.Value))
                                {
                                    checkBox.Checked = true;
                                    textBox.Text = values[i];
                                }
                            }
                            textBox.Enabled = checkBox.Checked;
                            required.Enabled = checkBox.Checked;
                        }
                    }
                    else
                    {
                        cocheTextBox.Text = star.DValues;
                    }
                }

                nameTextBox.Text = star.SName;

                if (star.Skill.HasValue)
                {
                    skillTextBox.Text = star.Skill.ToString();
                }
                if (star.PhysicalFitness.HasValue)
                {
                    fitnessTextBox.Text = star.PhysicalFitness.ToString();
                }
                if (star.Liveness.HasValue)
                {
                    livenessTextBox.Text = star.Liveness.ToString();
                }
                if (star.Moral.HasValue)
                {
                    moralTextBox.Text = star.Moral.ToString();
                }
                if (star.Price.HasValue)
                {
                    moneyTextBox.Text = (Convert.ToInt32(star.Price.Value)).ToString();
                }

                string[] strLevel = star.UsingLevel.Split(',');
                foreach (ListItem clItem in ulCheckBoxList.Items)
                {
                    for (int i = 0; i < strLevel.Length; i++)
                    {
                        if (strLevel[i].Equals(clItem.Value))
                        {
                            clItem.Selected = true;
                        }
                    }
                }
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLStar.Delete(id);
                LoadInitialValue();
                ClearPage();
            }
        }

        protected void sportTypeDropDown_Change(object sender, EventArgs e)
        {
            int id = int.Parse(sportTypeDropDown.SelectedValue);
            BindCheckBoxList(id);
            ScriptManager.RegisterStartupScript(this, typeof(Page), "ChangeType", "ChangeType()", true);
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.Public_Star BLLStarType = new Jy123.BLL.Public_Star();
            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.Public_Star star = new Jy123.Model.Public_Star();
                SetValue(star);
                star.ID = BLLStarType.Add(star);
                star.Model = CreateLogo(fileUploadBig.PostedFile, star.FSportType.Value, star.FSType.Value, star.ID, "Model");
                star.SmallModel = CreateLogo(fileUploadSmall.PostedFile, star.FSportType.Value, star.FSType.Value, star.ID, "SmallModel");
                BLLStarType.Update(star);
            }
            else
            {
                int id = int.Parse(idHiddenEdit.Value);
                Jy123.Model.Public_Star star = BLLStarType.GetModel(id);
                SetValue(star);
                if (fileUploadBig.PostedFile != null)
                {
                    string strLogo = CreateLogo(fileUploadBig.PostedFile, star.FSportType.Value, star.FSType.Value, star.ID, "Model");

                    if (!string.IsNullOrEmpty(strLogo))
                    {
                        star.Model = strLogo;
                    }

                }
                if (fileUploadSmall.PostedFile != null)
                {
                    string strSmallLogo = CreateLogo(fileUploadSmall.PostedFile, star.FSportType.Value, star.FSType.Value, star.ID, "SmallModel");
                    if (!string.IsNullOrEmpty(strSmallLogo))
                    {
                        star.SmallModel = strSmallLogo;
                    }
                }


                BLLStarType.Update(star);
            }
            LoadInitialValue();
            ClearPage();
        }

        private void LoadInitialValue()
        {
            Jy123.BLL.Public_Star BLLStar = new Jy123.BLL.Public_Star();
            List<Jy123.Model.Public_Star> listStar = BLLStar.GetModelList(string.Empty);
            StarRepeater.DataSource = listStar;
            StarRepeater.DataBind();
            StarRepeater.Visible = listStar.Count > 0;

            Jy123.BLL.Public_StarQueueName BLLQueueName = new Jy123.BLL.Public_StarQueueName();
            List<Jy123.Model.Public_StarQueueName> listQueueName = BLLQueueName.GetModelList(string.Empty);

            StarOptionRepeater.DataSource = listQueueName;
            StarOptionRepeater.DataBind();
            StarOptionRepeater.Visible = listQueueName.Count > 0;

            BindDropDown();
        }

        private void BindDropDown()
        {
            sportTypeDropDown.Items.Clear();
            sportTypeDropDown.DataSource = listSportType;
            sportTypeDropDown.DataTextField = "SName";
            sportTypeDropDown.DataValueField = "ID";
            sportTypeDropDown.DataBind();
            sportTypeDropDown.Items.Insert(0, new ListItem("请选择", "-1"));
            typeDropDown.Items.Clear();
            typeDropDown.DataSource = listStarType;
            typeDropDown.DataTextField = "SName";
            typeDropDown.DataValueField = "ID";
            typeDropDown.DataBind();
            typeDropDown.Items.Insert(0, new ListItem("请选择", "-1"));
        }

        private void BindCheckBoxList(int id)
        {
            ulCheckBoxList.Items.Clear();
            ulDiv.Visible = id > 0;
            if (id > 0)
            {
                string strWhere = string.Format("FJType =(SELECT ID FROM dbo.Public_JType WHERE FSportType ={0} AND JFather = 2)", id);
                Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
                List<Jy123.Model.Public_Level> listLevel = BLLLevel.GetModelList(strWhere);
                ulCheckBoxList.DataSource = listLevel;
                ulCheckBoxList.DataTextField = "LName";
                ulCheckBoxList.DataValueField = "ID";
                ulCheckBoxList.DataBind();
            }
        }

        private void ClearPage()
        {
            actionTypeHidden.Value = "Add";
            nameTextBox.Text = string.Empty;

            skillTextBox.Text = string.Empty;

            fitnessTextBox.Text = string.Empty;

            livenessTextBox.Text = string.Empty;

            moralTextBox.Text = string.Empty;

            expTextBox.Text = string.Empty;

            moneyTextBox.Text = string.Empty;            
        }

        private void SetValue(Jy123.Model.Public_Star star)
        {
            if (int.Parse(sportTypeDropDown.SelectedValue) > 0)
            {
                star.FSportType = int.Parse(sportTypeDropDown.SelectedValue);
            }
            if (int.Parse(typeDropDown.SelectedValue) > 0)
            {
                star.FSType = int.Parse(typeDropDown.SelectedValue);
                if (typeDropDown.SelectedItem.ToString().Equals("球星"))
                {
                    if (!string.IsNullOrEmpty(expTextBox.Text))
                    {
                        star.Experience = int.Parse(expTextBox.Text);
                    }
                    string strDes = ",";
                    string strValues = ",";
                    for (int j = 0; j < StarOptionRepeater.Items.Count; j++)
                    {
                        RepeaterItem optionItem = StarOptionRepeater.Items[j];
                        CheckBox checkBox = optionItem.FindControl("cNameCheckBox") as CheckBox;
                        TextBox textBox = optionItem.FindControl("valueTextBox") as TextBox;
                        HiddenField idHidden = optionItem.FindControl("idHiddenCondition") as HiddenField;
                        if (checkBox.Checked)
                        {
                            strDes += idHidden.Value + ",";
                            strValues += textBox.Text + ",";
                        }
                    }
                    if (strDes.Length > 1)
                    {
                        star.Description = strDes;
                        star.DValues = strValues;
                    }
                }
                else
                {
                    star.DValues = cocheTextBox.Text;
                    star.Description = string.Empty;
                }
            }
            star.SName = nameTextBox.Text;
            if (!string.IsNullOrEmpty(skillTextBox.Text))
            {
                star.Skill = int.Parse(skillTextBox.Text);
            }
            if (!string.IsNullOrEmpty(fitnessTextBox.Text))
            {
                star.PhysicalFitness = int.Parse(fitnessTextBox.Text);
            }
            if (!string.IsNullOrEmpty(livenessTextBox.Text))
            {
                star.Liveness = int.Parse(livenessTextBox.Text);
            }
            if (!string.IsNullOrEmpty(moralTextBox.Text))
            {
                star.Moral = int.Parse(moralTextBox.Text);
            }

            if (!string.IsNullOrEmpty(moneyTextBox.Text))
            {
                star.Price = decimal.Parse(moneyTextBox.Text);
            }
            string strLevel = ",";
            foreach (ListItem item in ulCheckBoxList.Items)
            {
                if (item.Selected)
                {
                    strLevel += item.Value + ",";
                }
            }
            if (strLevel.Length > 1)
            {
                star.UsingLevel = strLevel;
            }
            else
            {
                star.UsingLevel = string.Empty;
            }
        }

        protected string CreateLogo(HttpPostedFile postFile, int sportTypeId, int typeId, int starId, string modelType)
        {
            CreateDir createDir = new CreateDir();
            string logoPath = postFile.FileName;
            string logoName = string.Empty;
            if (!string.IsNullOrEmpty(logoPath))
            {
                string logoType = logoPath.Substring(logoPath.LastIndexOf(".") + 1);
                logoName = createDir.CreateStarPath(CreateDir.STAR, sportTypeId, typeId, starId, logoType, modelType);
                string mapPath = Server.MapPath("/");

                Upload upload = new Upload();
                upload.PostedFile = postFile;
                upload.FileLength = postFile.ContentLength;
                upload.FileName = logoName;
                upload.Extension = logoType;
                upload.SavePath = mapPath;
                upload.UploadStart(logoName);
            }
            return logoName;
        }
    }
}
